package com.hxbd.test.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Assert;

import com.duan.test.utils.common.CommonUtils;
import com.hxbd.test.handles.PageElementHandler;

public class ReadConfigFromXml {
	static Element root;
    static Document document = null;
    private static final String INTER_RULE_PATH = "INTER_RULE_PATH";
//	static {
//		//创建SAXReader对象  
//	    SAXReader reader = new SAXReader();  
//	    //读取文件 转换成Document  
//	    try {
//			document = reader.read(CommonUtils.getFilePath(PageElementHandler.getStoreSettingProperties().getProperty(INTER_RULE_PATH)));
//		} catch (DocumentException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}  		
//		//获取根节点元素对象  
//	    root = document.getRootElement();  
//	}
	@SuppressWarnings("unchecked")
	public static String getReqstType (String proname) {
		List<Element> l = root.element("projects").elements("project");
		String type = null;
		for (Element e : l) {
			if (e.attributeValue("name").equalsIgnoreCase(proname)) {
				type = e.attributeValue("type");
				break;
			}
		}
		return type;
	}
	public static List<Node> getElementsFromXpath(String xpath) {
		List<Node> list=document.selectNodes(xpath);
		return list;
	}
	public static Node getSingleElementFromXpath(String xpath) {
		Node list=document.selectSingleNode(xpath);
		return list;
	}
	public static Map<String,String> getParamsDesc() {
		Map<String,String> m = new HashMap<String,String>();
		List<Element> l = root.element("paramsDescs").elements("desc");
		for (Element e : l) {
			m.put(e.attributeValue("index"), e.attributeValue("value"));
		}
		return m;
	}
	public static Map<String,Object> readInterfaceConfig(String proname) {
		Map<String,Object> m = new HashMap<String,Object>();
		List<Element> l = root.element("projects").elements("project");
		Boolean canfoundproject = false;
		for (Element e : l) {
			if (e.attributeValue("name").equalsIgnoreCase(proname)) {
				String type = e.attributeValue("type");
				if (type.equalsIgnoreCase("rpc")) {
					Map<String,String> tmpmap2 = new HashMap<String,String>();
					tmpmap2.put("code", e.element("return").attributeValue("code"));
					tmpmap2.put("message", e.element("return").attributeValue("message"));
					tmpmap2.put("data", e.element("return").attributeValue("data"));
					m.put("return", tmpmap2);
				}
				else if (type.equalsIgnoreCase("httpclient")) {
					Map<String,String> tmpmap2 = new HashMap<String,String>();
					tmpmap2.put("code", e.element("return").attributeValue("code"));
					tmpmap2.put("message", e.element("return").attributeValue("message"));
					tmpmap2.put("data", e.element("return").attributeValue("data"));
					m.put("return", tmpmap2);
					m.put("sendtype", e.element("sendtype").attributeValue("name"));
					m.put("path", e.element("path").attributeValue("name"));
					Map<String,String> tmpmap3 = new HashMap<String,String>();
					List<Element> headers = e.element("header").elements();
					for (Element header : headers) {
						tmpmap3.put(header.attributeValue("name"), header.attributeValue("value"));
					}
					m.put("header", tmpmap3);
					Map<String,String> tmpmap4 = new HashMap<String,String>();
					List<Element> bodys = e.element("body").elements();
					for (Element body : bodys) {
						tmpmap4.put(body.attributeValue("name"), body.attributeValue("value"));
					}
					m.put("body", tmpmap4);
				}
				canfoundproject = true;
				break;
			}
		}
		if (!canfoundproject) {
			Assert.fail("找不到对应的project");
		}
		return m;
	}
}
